// @algorithm @lc id=100321 lang=cpp
// @title shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof

#include "algm/algm.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
    // @test([3,4,3,3])=4
public:
    // 数组存储解法
    /* int singleNumber(vector<int> &nums) {
        int n = nums.size();
        vector<int> bitSum(32, 0);
        for (int i = 0; i < n; ++i) {
            unsigned int bitMask = 1;
            for (int j = 31; j >= 0; --j) {
                int bit = nums[i] & bitMask;
                if (bit != 0) {
                    bitSum[j] += 1;
                }
                bitMask = bitMask << 1;
            }
        }
        int ans = 0;
        for (int i = 0; i < 32; ++i) {
            ans = ans << 1;
            ans += bitSum[i] % 3;
        }
        return ans;
    } */

    // 位运算状态机解法
    int singleNumber(vector<int> &nums) {
        int a = 0, b = 0;
        for (auto &num : nums) {
            b = (b ^ num) & ~a;
            a = (a ^ num) & ~b;
        }
        return b;
    }
};